<template>
	<view>
		<uni-popup ref="commendRef" type="bottom" :maskClick="false">
			<view class="popup-container">
				<view class="body">
					<view class="tray_data">
						<uni-forms :modelValue="trayData">
							<uni-forms-item label="件号:" name="itemnumber" style="width: 250px;">
								<uni-easyinput type="text" v-model="trayData.itemnumber" />
							</uni-forms-item>
							<uni-forms-item label="仓库" name="stockNo" style="width: 200px;">
								<uni-data-select v-model="trayData.stockNo" :localdata="stockList" @change="change"
									placeholder="请选择仓库"></uni-data-select>
							</uni-forms-item>
							<uni-forms-item label="批号:" name="flot" style="width: 200px;">
								<uni-easyinput type="text" v-model="trayData.flot" />
							</uni-forms-item>
							<uni-forms-item label="生产:" name="produceDate" style="width: 250px;">
								<uni-easyinput type="text" v-model="trayData.produceDate" />
							</uni-forms-item>
							<uni-forms-item label="工单号:" name="foperpno" style="width: 200px;">
								<uni-easyinput type="text" v-model="trayData.foperpno" />
							</uni-forms-item>
							<uni-forms-item label="物料编码:" name="mateiralNo" style="width: 200px;">
								<uni-easyinput type="text" v-model="trayData.mateiralNo" />
							</uni-forms-item>
							<uni-forms-item label="数量:" name="fqty" style="width: 200px;">
								<uni-easyinput type="number" v-model="trayData.fqty" />
							</uni-forms-item>
							<button @click="selectInventory" class="footerBtn query-btn">
								<i></i>查询
							</button>
						</uni-forms>
					</view>
				</view>
				<!-- 将按钮区域移到这里，确保在popup内部但不在滚动区域内 -->
				<view class="footerPanel">
					<button @click="closeInfoPop" class="footerBtn concle">
						<i></i>取消
					</button>
					<button @click="addTray" class="footerBtn save">
						<i class="iconfont icon-duihao btn_icon"></i>新增
					</button>
				</view>
			</view>
		</uni-popup>
		<!-- 扫描仓位码后弹出领料界面，可以修改领料信息 -->
		<add-pick ref="pickRef"></add-pick>
		<!-- 推荐仓位-->
		<Commend ref="commendAddRef" @back="back"></Commend>
	</view>
</template>

<script>
	import Commend from "@/pages/pop/commend-scanFeed.vue"
	import {
		createTrayPdaByKu,
		commendLocationByItem,
		getStockListByUser
	} from '@/utils/api.js'
	import AddPick from "@/pages/pop/add-pick-commend.vue"

	export default {
		components: {
			AddPick,
			Commend
		},
		data() {
			return {
				trayData: {
					itemnumber: '',
					stockNo: '',
					stockName: '',
					flot: '',
					foperpno: '',
					mateiralNo: '',
					fqty: ''
				}, //托盘信息
				trayCount: 0, //托盘数量
				planData: {}, //工序计划数据
				type: '',
				stockList: [],

				commedList: []
			};
		},
		methods: {
			//修改仓库后重新校验仓库有没有启用仓位
			change(value) {
				const found = this.stockList.find(item => item.value === value)
				if (found) {
					this.trayData.stockName = found.text
				}
			},
			/**
			 * @param {Object} e库存查询返回物料编码
			 */
			back(item) {
				this.$set(this.trayData, 'mateiralNo', item.fmaterialNo)
				this.$set(this.trayData, 'flot', item.flot)
				this.$set(this.trayData, 'produceDate', item.produceDate)
				this.$set(this.trayData, 'locationNo', item.locationNo)
				this.$set(this.trayData, 'stockNo', item.stockNo)
				this.$set(this.trayData, 'fqty', item.FBASEQTY)
			},
			/**
			 * 查询库存
			 */
			selectInventory() {
				let self = this;
				commendLocationByItem({
					itemnumber: this.trayData.itemnumber,
					stockNo: this.trayData.stockNo,
				}).then(res => {
					if (res.data.status == 200) {
						self.commedList = res.data.data.dataList ?? []
					}
					let param = {
						dataList: self.commedList
					}

					this.$refs.commendAddRef.open(param)
				})

			},
			getStockList() {
				getStockListByUser({}).then(res => {
					const list = res.data.data.dataList || []
					this.stockList = list.map(item => ({
						text: item.stockName,
						value: item.stockNo,
					}))
					// ✅ 这里非常关键：在 DOM 更新后再设置默认值
					this.$nextTick(() => {
						const defaultStock = this.stockList.find(s => s.value === 'CK004') || this.stockList[0]
						if (defaultStock) {
							this.trayData.stockNo = defaultStock.value
							this.trayData.stockName = defaultStock.text
						}
					})
				})
			},
			//修改托盘数量
			addTray() {
				let _this = this
				if (!this.trayData.flot) {
					uni.showToast({
						icon: 'error',
						title: '请输入批号'
					})
					return;
				}
				uni.showModal({
					title: '提示',
					content: '确认新增',
					success(res) {
						if (res.confirm) {
							_this.trayData.itemnumber = _this.trayData.mateiralNo
							createTrayPdaByKu(_this.trayData).then(result => {

								if (result.data) {
									uni.showToast({
										title: '托盘新增成功',
										icon: 'none'
									})
								}
								setTimeout(() => {
									_this.close()
									window.location.reload()
								}, 2000)


							})

						}
					}
				})
			},


			updateFqty(e) {
				if (e.pickCount > e.FBASEQTY) {
					e.pickCount = e.FBASEQTY
				}
			},


			closeInfoPop() {
				this.$emit('back', true)
				this.$refs.commendRef.close()
			},

			open(data) {
				// this.trayData = data.trayData
				this.$refs.commendRef.open('center')

			},
			close() {
				this.$emit('back', true)
				this.$refs.commendRef.close()
			}
		},
		mounted() {
			this.getStockList()
			setTimeout(() => {

			}, 1000)
		},
		onLoad() {

		}
	}
</script>

<style lang="scss">
	.popup-container {
		display: flex;
		flex-direction: column;
		height: 80vh;
		/* 限制弹窗高度 */
	}

	.body {
		background-color: aliceblue;
		flex: 1;
		/* 占据剩余空间 */
		overflow-y: auto;
		/* 允许内容滚动 */
		padding-bottom: 10px;
	}

	.tray_data {
		margin-top: 20px;
		/* 减小上边距 */
		width: 100%;

		/* 调整表单项间距 */
		::v-deep .uni-forms-item {
			margin-bottom: 8px !important;
			/* 减小行间距 */
			min-height: 36px !important;
			/* 减小最小高度 */

			&__label {
				font-size: 18px !important;
				margin-bottom: 2px !important;
				/* 标签与输入框间距 */
			}
		}
	}

	.uni-forms-item {
		align-items: flex-start !important;
		/* 顶部对齐 */
		min-height: 36px;
		/* 设置统一高度 */
	}

	/* 调整标签字体大小 */
	.tray_data ::v-deep .uni-forms-item__label {
		padding-left: 10px;
		font-size: 20px !important;
		color: #000000 !important;
		/* 纯黑字体 */
	}


	.tray_data ::v-deep .uni-forms-item input {
		font-size: 20px !important;
	}

	/* 特殊处理数量输入框 */
	.tray_data ::v-deep .uni-easyinput {
		height: 50px;
		font-size: 30px !important;
		width: 120% !important;
		/* 保持原有输入框宽度 */
	}

	/* 调整标签组件样式 */
	.tray_data ::v-deep .uni-tag {
		font-size: 18px !important;
		margin-left: 8px;
		/* 调整标签与文字间距 */
	}

	.uni-forms-item ::v-deep .uni-forms-item__label {
		white-space: nowrap;
		/* 禁止换行 */
		min-width: 100px !important;
		/* 设置最小宽度 */
	}

	/* 查询按钮样式 */
	.query-btn {
		height: 50px;
		width: 90%;
		margin: 20px auto;
		display: block;
		font-size: 20px;
		background-color: #007AFF; /* 使用具体颜色值代替变量 */
		color: white;
		border-radius: 8px;
		border: none;
	}

	/* 底部按钮 - 固定在底部 */
	.footerPanel {
		height: 70px;
		background-color: aliceblue;
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 10px 20px;
		border-top: 1px solid #e0e0e0;
		box-sizing: border-box;

		.footerBtn {
			height: 50px;
			min-width: 120px;
			display: flex;
			line-height: 1;
			font-size: 20px;
			border-radius: 8px;
			align-items: center;
			justify-content: center;
			box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
			color: #fff;
			border: none;

			/* 取消按钮 */
			&.concle {
				background-color: #a0a0a0;
			}

			/* 提交按钮 */
			&.save {
				background-color: #4CD964; /* 使用具体颜色值代替变量 */
			}
		}
	}
</style>